const express = require("express");
const jobsRouter = express.Router();
const DB = require("./../db/config");

module.exports = jobsRouter;
/**
 * 添加医生职称
 * @param {String} jobName required,
 * @param {String} jobDes required
 *
 */
jobsRouter.route("/jobs/add").post((req, res) => {
  let { jobName, jobDes } = req.body;
  let sql = "insert into jobs (jname,jdes) values(?,?);";
  let param = [jobName, jobDes];
  let cb = (err, data) => {
    if (!err) {
      res.json({ code: 0, msg: "添加职称成功", data: [] });
    } else {
      res.json({ code: 4002, msg: "添加职称失败", data: [] });
    }
  };
  DB.connect(sql, param, cb);
});
/**
 * 获取所有医生职称数据
 */
jobsRouter.route("/jobs/all").get((req, res) => {
  let sql = "select * from jobs;";
  let cb = (err, data) => {
    if (!err) {
      res.json({ code: 0, msg: "查询所有医生职称成功", data });
    } else {
      res.json({ code: 4002, msg: "查询所有医生职称失败", data });
    }
  };
  DB.connect(sql, [], cb);
});
/**
 * 修改医生职称
 * @param {Number} jid 医生职称ID requred
 * @param {String} jdes 医生职称描述 requred
 * @param {String} jname 医生职称名字 requred
 */
jobsRouter.route("/jobs/modify").post((req, res) => {
  let { jname, jdes, jid } = req.body;
  let param = [jname, jdes, jid];
  let sql = "update jobs set jname = ?, jdes = ? where jid = ?;";
  let cb = (err, data) => {
    if (!err) {
      res.json({ code: 0, msg: "修改医生职称成功。", data: [] });
    } else {
      res.json({ code: 4002, msg: "修改医生职称失败。", data: [] });
    }
  };
  DB.connect(sql, param, cb);
});
/**
 * 删除一个医生职称
 * @param {Number} jid 医生职称ID required
 */
jobsRouter.route('/jobs/del')
.post((req, res)=>{
    let jid = req.body.jid;
    let param = [jid]
    let sql = "delete from jobs where jid = ?;";
    let cb = (err, data)=>{
        if (!err) {
            res.json({code:0,msg:'删除医生职称成功',data:[]})
        } else {
            res.json({code:4002,msg:'删除医生职称失败',data:[]})
            
        }
    };
    DB.connect(sql, param, cb);

})